package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hitqz.robot.api.common.entity.Robot;
import com.hitqz.robot.api.common.entity.SysWarning;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotWarningDto;
import com.hitqz.robot.biz.mapper.SysWarningMapper;
import com.hitqz.robot.biz.service.RobotService;
import com.hitqz.robot.biz.service.SysWarningService;
import com.hitqz.robot.netty.service.Send2ServerService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xupkun
 * @date 2024/6/25
 */
@Service
@AllArgsConstructor
public class SysWarningServiceImpl extends ServiceImpl<SysWarningMapper, SysWarning> implements SysWarningService {

    private final Send2ServerService send2ServerService;

    private final RobotService robotService;

    @Override
    public void syncRobotWarnings(Integer id) {
        List<SysWarning> sysWarnings = Lists.newArrayList();
        if (id == null) {
            sysWarnings = baseMapper.selectList(null);
        } else {
            sysWarnings.add(baseMapper.selectById(id));
        }
        Robot robot = robotService.getDefaultRobot();
        for (SysWarning sysWarning:  sysWarnings) {
            RobotWarningDto robotWarningDto = new RobotWarningDto();
            BeanUtil.copyProperties(sysWarning, robotWarningDto);
            send2ServerService.syncSysWarning(robot, robotWarningDto);
        }
    }


}
